package com.egao.common.manage.student;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.student.constants.StudentConstants;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentBasic;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.StudentSignSet;
import com.egao.common.module.student.service.StudentBasicService;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.module.student.service.StudentSignSetService;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 学校基层
 * @author lsh
 */
@Api(tags = "学校基层")
@RestController
@RequestMapping("${api}")
public class StudentBasicController extends BaseController {

    @Autowired
    private StudentService studentService;
    @Autowired(required = false)
    private StudentBasicService studentBasicService;
    @Autowired
    private StudentSignService studentSignService;
    @Autowired
    private DictService dictService;
    @Autowired
    private StudentSignSetService studentSignSetService;

    private final String title = "学生基层就业填报管理";

    @PreAuthorize("hasAuthority('student')")
    @ApiOperation(value = "学生基层就业填报详情")
    @ApiPageParam
    @GetMapping("/student/basic/detail")
    public JsonResult activityPage(HttpServletRequest request) {
        Student student = studentService.getByUserId(getLoginUserId());

        if (student == null) {
            return JsonResult.error("没有找到学生数据");
        }
        StudentBasic studentBasic = studentBasicService.BasicDetails(student);
        boolean bool = false;
        String companyName = "";
        StudentSign stateValid = studentSignService.getStateValid(getLoginUserId());
        if (stateValid != null
                && StudentSignConstants.SIGN_STATE_SCHOOL_PASS.equals(stateValid.getSignState())
                && StudentSignConstants.DISPATCH_STATE_SCHOOL_PASS.equals(stateValid.getDispatchState())
                && (StudentSignConstants.SIGN_CATEGORY_XYJY.equals(stateValid.getSignCategory())
                || StudentSignConstants.SIGN_CATEGORY_ZZCY.equals(stateValid.getSignCategory())
                || StudentSignConstants.SIGN_CATEGORY_SX.equals(stateValid.getSignCategory()))
        ) {
            bool = true;
            if (StudentSignConstants.SIGN_CATEGORY_XYJY.equals(stateValid.getSignCategory())){
                companyName = stateValid.getDealSignCompanyName();
            }
            if (StudentSignConstants.SIGN_CATEGORY_ZZCY.equals(stateValid.getSignCategory())){
                companyName = stateValid.getVentureCompanyName();
            }
            if (StudentSignConstants.SIGN_CATEGORY_SX.equals(stateValid.getSignCategory())){
                String label = dictService.cacheLabel("dc_lqyxdm", stateValid.getStudySchoolName());
                companyName = label;
            }

        }

        StudentSignSet studentSignSet = studentSignSetService.get();
        return JsonResult.ok().setData(studentBasic)
                .put("bool", bool)
                .put("companyName",companyName)
                .put("basic", studentSignSet.getBasic())
                ;
    }

    @PreAuthorize("hasAuthority('student')")
    @ApiOperation(value = "学生基层新增")
    @ApiPageParam
    @PostMapping("/student/basic/save")
    public JsonResult basicSave(@RequestBody StudentBasic studentBasic) {
        studentBasic.setState("0");
        studentBasic.setStateContent("");
        boolean save = studentBasicService.saveOrUpdate(studentBasic);
        if (save){
            return JsonResult.ok();
        }
        return JsonResult.error("服务器异常，请联系管理人员！");
    }

}
